'use strict';

const Controller = require('egg').Controller;
const md5 = require('md5');


class AddressController extends Controller {
  async index() {
    const { query } = this.ctx.request;
    const { latitude, longitude } = query; // 28.7033487,115.8660847
    // const latitude = '28.7033487';
    // const longitude = '115.8660847';
    console.log('latitude', latitude);
    console.log('longitude', longitude);
    if (latitude && longitude) {
      const key = 'DUUBZ-AZH6X-7BH4Q-ZCEYM-R4DBE-4XBZB';
      const location = `${latitude},${longitude}`;
      const sk = 'UhMvnXanC4SO6gg1zZbOIe0wi4jizf';
      const sig = md5(`/ws/geocoder/v1?key=${key}&location=${location}${sk}`);
      console.log('sigsigsig', sig);
      const url = `https://apis.map.qq.com/ws/geocoder/v1?key=${key}&location=${location}&sig=${sig}`;
      const addressBuffer = await this.ctx.curl(url);
      console.log('addressaddressaddress', addressBuffer);
      const stringAddress = addressBuffer.data.toString();
      const jasonAddress = JSON.parse(stringAddress);
      console.log('addressaddressaddressaddressjasonAddress', jasonAddress.result.address_component);
      this.ctx.status = 200;
      this.ctx.body = jasonAddress.result.address_component;
    }
  }
}

module.exports = AddressController;
